﻿using System;
using System.Linq;
using System.IO;
using System.Text.RegularExpressions;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.Odbc;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Reviewer.Entities.Services;
using Reviewer.Business;
using Reviewer.Entities;
using System.Collections.Generic;

public partial class public_html_NewSiteTest_Releaseliste : System.Web.UI.Page
{
    public int ScreenshotId
    {
        get
        {
            if (ViewState["PicID"] != null)
            {
                return (int)ViewState["PicID"];
            }
            if (!String.IsNullOrEmpty(Request["PicID"]))
            {
                int val;
                if (int.TryParse(Request["PicID"], out val))
                {
                    ViewState["PicID"] = val;
                    return val;
                }
            }
            return -1;
        }
        set
        {
            ViewState["PicID"] = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (this.ScreenshotId > 0)
            {
                using (IGameScreenshotService service = ServiceFactory.GameScreenshotService)
                {
                    GameScreenshot entity = service.Find(this.ScreenshotId);

                    this.uiImgSelectedImage.ImageUrl = UrlUtillity.GetGamescreenshotImageUri(entity);

                    IEnumerable<GameScreenshot> otherScreenshots = service.Find(entity.GameId).Where(t => t.Format.Equals(entity.Format));
                    this.uiBtnNext.Enabled = this.uiBtnPrevious.Enabled = true;
                    this.uiRepScreenshots.DataSource = otherScreenshots;
                    this.uiRepScreenshots.DataBind();
                    ViewState["PicID"] = this.ScreenshotId;


                    using (IGameService gameService = ServiceFactory.GameService)
                    using (IGameViewService gameViews = ServiceFactory.GameViewService)
                    {
                        Game game = gameService.GetBy(entity.GameId).FirstOrDefault(t => t.Format.Equals(entity.Format));
                        this.uiLblHeadline.Text = this.Title = "Screenshots - " + game.Title;

                        //Log visit
                        gameViews.LogView(game.Id, game.GameId);
                    }
                }
            }
        }
    }

    protected void uiRepScreenshots_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        switch (e.Item.ItemType)
        {
            case ListItemType.AlternatingItem:
            case ListItemType.Item:
                GameScreenshot entity = e.Item.DataItem as GameScreenshot;
                HyperLink link = e.Item.FindControl("uiHlScreenshot") as HyperLink;
                Image image = e.Item.FindControl("uiImgScreenshot") as Image;
                link.NavigateUrl = UrlUtillity.GetGameScreenshotUri(entity);
                image.ImageUrl = UrlUtillity.GetGamescreenshotImageUri(entity);
                if (entity.Id.Equals(this.ScreenshotId))
                {
                    image.Attributes.Add("style", "border:4px solid #EEEEEE;");
                    image.Width = Convert.ToInt32(image.Width.Value * 1.25);
                    image.Height = Convert.ToInt32(image.Height.Value * 1.25);
                    if (e.Item.ItemIndex.Equals(0))
                    {
                        this.uiBtnPrevious.Enabled = false;
                    }
                    else if (e.Item.ItemIndex.Equals(((e.Item.Parent as Repeater).DataSource as IEnumerable<GameScreenshot>).Count() - 1))
                    {
                        this.uiBtnNext.Enabled = false;
                    }
                }
                else
                {
                    image.Attributes.Add("style", "");
                }
                break;
        }
    }


    protected void uiBtnNext_Click(object sender, EventArgs e)
    {
        using (IGameScreenshotService service = ServiceFactory.GameScreenshotService)
        {
            GameScreenshot entity = service.Find(this.ScreenshotId);
            IEnumerable<GameScreenshot> otherScreenshots = service.Find(entity.GameId).Where(t => t.Format.Equals(entity.Format));
            this.uiBtnNext.Enabled = this.uiBtnPrevious.Enabled = true;

            for (int i = 0; i < otherScreenshots.Count(); i++)
            {
                if (otherScreenshots.ElementAt(i).Id.Equals(this.ScreenshotId))
                {
                    i++;
                    this.ScreenshotId = otherScreenshots.ElementAt(i).Id;
                    this.uiImgSelectedImage.ImageUrl = UrlUtillity.GetGamescreenshotImageUri(otherScreenshots.ElementAt(i));
                    break;
                }
            }

            this.uiRepScreenshots.DataSource = otherScreenshots;
            this.uiRepScreenshots.DataBind();
        }

    }

    protected void uiBtnPrevious_Click(object sender, EventArgs e)
    {
        using (IGameScreenshotService service = ServiceFactory.GameScreenshotService)
        {
            GameScreenshot entity = service.Find(this.ScreenshotId);
            IEnumerable<GameScreenshot> otherScreenshots = service.Find(entity.GameId).Where(t => t.Format.Equals(entity.Format));
            this.uiBtnNext.Enabled = this.uiBtnPrevious.Enabled = true;

            for (int i = 0; i < otherScreenshots.Count(); i++)
            {
                if (otherScreenshots.ElementAt(i).Id.Equals(this.ScreenshotId))
                {
                    i--;
                    this.ScreenshotId = otherScreenshots.ElementAt(i).Id;
                    this.uiImgSelectedImage.ImageUrl = UrlUtillity.GetGamescreenshotImageUri(otherScreenshots.ElementAt(i));
                    break;
                }
            }
            this.uiRepScreenshots.DataSource = otherScreenshots;
            this.uiRepScreenshots.DataBind();
        }
    }
}